home *** CD-ROM | disk | FTP | other *** search
/ PD Collection CD 1 / PD Collection CD 1.iso / textual / tex / files / !tex / TeXsource / commontex / c / fmt < prev    next >
Encoding:
Text File  |  1988-04-18  |  14.9 KB  |  465 lines

  1. /*
  2.  *    Copyright 1986, 1987 Pat Joseph Monardo. All rights reserved.
  3.  *    Copying of this file is granted according to the provisions 
  4.  *    specified in the file COPYING which must accompany this file.
  5.  */
  6.  
  7.  
  8. /*
  9.  *              fmt.c
  10.  */
  11.  
  12. #include "tex.h"
  13. #include "texext.h"
  14. #include "heap.h"
  15. #include "token.h"
  16. #include "eq.h"
  17. #include "box.h"
  18. #include "eqstack.h"
  19. #include "hash.h"
  20. #include "file.h"
  21. #include "tfm.h"
  22. #include "str.h"
  23. #include "hyph.h"
  24. #include "token.h"
  25. #include "io.h"
  26. #include "print.h"
  27. #include "error.h"
  28. #include "fmt.h"
  29.  
  30. word_file       fmt_file;
  31.  
  32. str             format_ident;
  33.  
  34. #define undump_ascii()          (str_pool[k] = getc(fmt_file))
  35. #define undump_int(M)           M = getw(fmt_file)
  36. #define undump_wd(M)            fread(&M, sizeof(M), 1, fmt_file);
  37. #define undump_hh(M)            fread(&M, sizeof(M), 1, fmt_file);
  38. #define undump_qqqq(M)          fread(&M, sizeof(M), 1, fmt_file);
  39.  
  40. #define undump(MIN, MAX, X) \
  41.         {undump_int(x); \
  42.         if (x < MIN || x > MAX) \
  43.                 goto bad_fmt; \
  44.         else X = x;}
  45.  
  46. #define too_small(S) \
  47.         printf("---! Must increase the %s\n", S)
  48.  
  49. #define undump_size(MIN, MAX, TABLE, X) \
  50.         {undump_int(x); \
  51.         if (x < MIN) goto bad_fmt; \
  52.         if (x > MAX) too_small(TABLE); \
  53.         else X = x;}
  54.  
  55. bool
  56. load_fmt_file ()
  57. {
  58.         int             j;
  59.         int             k;
  60.         ptr             p;
  61.         ptr             q;
  62.         int             w;
  63.         int             x;
  64.  
  65.         undump_int(x);
  66.         if (x != MEM_BOT) goto bad_fmt;
  67.         undump_int(x);
  68.         if (x != MEM_TOP) goto bad_fmt;
  69.         undump_int(x);
  70.         if (x != TOK_BOT) goto bad_fmt;
  71.         undump_int(x);
  72.         if (x != TOK_TOP) goto bad_fmt;
  73.         undump_int(x);
  74.         if (x != EQTB_SIZE) goto bad_fmt;
  75.         undump_int(x);
  76.         if (x != HASH_PRIME) goto bad_fmt;
  77.         undump_int(x);
  78.         if (x != HYPH_SIZE) goto bad_fmt;
  79.  
  80.         undump_int(pool_ptr);
  81.         undump_int(str_ptr);
  82.         undump_int(null_str);
  83.         for (k = 0; k <= str_ptr; incr(k))
  84.                 undump(0, pool_ptr, str_start[k]);
  85.         for (k = 0; k < pool_ptr; incr(k))
  86.                 undump_ascii();
  87.  
  88.         undump(LO_MEM_STAT_MAX + 1000, HI_MEM_STAT_MIN - 1, lo_mem_max);
  89.         undump(LO_MEM_STAT_MAX  + 1, lo_mem_max, rover);
  90.         p = MEM_BOT;
  91.         q = rover;
  92.         x = 0;
  93.         do {
  94.                 for (k = p; k <= q + 1; incr(k))
  95.                         undump_wd(mem[k]);
  96.                 p = q + node_size(q);
  97.                 if (p > lo_mem_max || q >= rlink(q) && rlink(q) != rover)
  98.                                 goto bad_fmt;
  99.                 q = rlink(q);
  100.         } while (q != rover);
  101.         for (k = p; k <= lo_mem_max; incr(k))
  102.                 undump_wd(mem[k]);
  103.  
  104.         undump(lo_mem_max + 1, HI_MEM_STAT_MIN, hi_mem_min);
  105.         undump(NULL, MEM_TOP, avail);
  106.         mem_end = MEM_TOP;
  107.         for (k = hi_mem_min; k <= mem_end; incr(k))
  108.                 undump_wd(mem[k]);
  109.  
  110.         undump(TOK_BOT, tok_high, tok_low);
  111.         undump(NULL, TOK_TOP, tok_head);
  112.         tok_end = TOK_TOP;
  113.         for (k = tok_low; k <= tok_end; incr(k)) {
  114.                 undump_wd(tok_link[k]);
  115.                 undump_wd(tok[k]);
  116.         }
  117.  
  118.         undump_int(var_used);
  119.         undump_int(dyn_used);
  120.         undump_int(tok_used);
  121.  
  122.         k = ACTIVE_BASE;
  123.         do {
  124.                 undump_int(x);
  125.                 if (x < 1 || k + x > EQTB_SIZE + 1)
  126.                         goto bad_fmt;
  127.                 for (j = k; j < k + x; incr(j))
  128.                         undump_wd(eqtb[j]);
  129.                 k += x;
  130.                 undump_int(x);
  131.                 if (x < 0 || k + x > EQTB_SIZE + 1)
  132.                         goto bad_fmt;
  133.                 for (j = k; j < k + x; incr(j)) 
  134.                         eqtb[j] = eqtb[k - 1];
  135.                 k += x;
  136.         } while (k <= EQTB_SIZE);
  137.         undump(HASH_BASE, FROZEN_CONTROL_SEQUENCE, par_loc);
  138.         par_token = CS_TOKEN_FLAG + par_loc;
  139.         undump(HASH_BASE, FROZEN_CONTROL_SEQUENCE, write_loc);
  140.  
  141.         undump(HASH_BASE, FROZEN_CONTROL_SEQUENCE, hash_used);
  142.         p = HASH_BASE - 1;
  143.         do {
  144.                 undump(p + 1, hash_used, p);
  145.                 undump_hh(hash[p]);
  146.         } while (p != hash_used);
  147.         for (p = hash_used + 1; p < UNDEFINED_CONTROL_SEQUENCE; incr(p))
  148.                 undump_hh(hash[p]);
  149.         undump_int(cs_count);
  150.  
  151.         undump_size(7, FONT_MEM_SIZE, "font mem size", fmem_ptr);
  152.         for (k = 0; k < fmem_ptr; incr(k))
  153.                 undump_wd(font_info[k]);
  154.         undump_size(FONT_BASE, FONT_MAX, "font max", font_ptr);
  155.         for (k = NULL_FONT; k <= font_ptr; incr(k)) {
  156.                 undump_qqqq(font_check[k]);
  157.                 undump_wd(font_size[k]);
  158.                 undump_wd(font_dsize[k]);
  159.                 undump(MIN_HALFWORD, MAX_HALFWORD, font_params[k]);
  160.                 undump_int(hyphen_char[k]);
  161.                 undump_int(skew_char[k]);
  162.                 undump(0, str_ptr, font_name[k]);
  163.                 undump(0, str_ptr, font_area[k]);
  164.                 undump(0, 255, font_bc[k]);
  165.                 undump(0, 255, font_ec[k]);
  166.                 undump_int(char_base[k]);
  167.                 undump_int(width_base[k]);
  168.                 undump_int(height_base[k]);
  169.                 undump_int(depth_base[k]);
  170.                 undump_int(italic_base[k]);
  171.                 undump_int(lig_kern_base[k]);
  172.                 undump_int(kern_base[k]);
  173.                 undump_int(exten_base[k]);
  174.                 undump_int(param_base[k]);
  175.                 undump(MIN_HALFWORD, hi_mem_min, font_glue[k]);
  176.         }
  177.  
  178.         undump(0, HYPH_SIZE, hyph_count);
  179.         for (k = 1; k <= hyph_count; incr(k)) {
  180.                 undump(0, HYPH_SIZE, j);
  181.                 undump(0, str_ptr, hyph_word[j]);
  182.                 undump(MIN_HALFWORD, MAX_HALFWORD, hyph_list[j]);
  183.         }
  184.         undump_size(0, TRIE_SIZE, "trie size", trie_max);
  185.         for (k = 0; k <= trie_max; incr(k))
  186.                 undump_hh(trie[k]);
  187.         undump(MIN_QUARTERWORD, MAX_QUARTERWORD, trie_op_ptr);
  188.         for (k = MIN_QUARTERWORD + 1; k <= trie_op_ptr; incr(k))  {
  189.                 undump(0, 63, hyf_distance[k]);
  190.                 undump(0, 63, hyf_num[k]);
  191.                 undump(MIN_QUARTERWORD, MAX_QUARTERWORD, hyf_next[k]);
  192.         }
  193.         undump(BATCH_MODE, ERROR_STOP_MODE, interaction);
  194.         undump(0, str_ptr, format_ident);
  195.         undump_int(x);
  196.         if (x != 6969 || feof(fmt_file)) goto bad_fmt;
  197.         return TRUE;
  198.  
  199. bad_fmt:
  200.         print("(Fatal format file error; I'm stymied)");
  201.         return FALSE;
  202. }
  203.  
  204. #ifdef INIT
  205.  
  206. #define dump_ascii()    putc(str_pool[k], fmt_file)
  207. #define dump_int(W)             putw(W, fmt_file)
  208. #define dump_hh(W)              fwrite(&W, sizeof(W), 1, fmt_file);
  209. #define dump_wd(W)              fwrite(&W, sizeof(W), 1, fmt_file);
  210. #define dump_qqqq(W)    fwrite(&W, sizeof(W), 1, fmt_file);
  211.  
  212. store_fmt_file ()
  213. {
  214.         int             j;
  215.         int             k;
  216.         int             l;
  217.         ptr             p;
  218.         ptr             q;
  219.         fourq   w;
  220.         int             x;
  221.  
  222.         if (save_ptr != 0) {
  223.                 print_nl("! You can't \\dump inside a group");
  224.                 help1("`{...\\dump}' is a no-no.");
  225.                 succumb();
  226.         }
  227.         selector = NEW_STRING;
  228.         print(" (preloaded format=");
  229.         print_str(job_name);
  230.         print_char(' ');
  231.         print_val(abs(year) % 100);
  232.         print_char('.');
  233.         print_val(month);
  234.         print_char('.');
  235.         print_val(day);
  236.         print_char(')');
  237.         if (interaction == BATCH_MODE)
  238.                 selector = LOG_ONLY;
  239.         else selector = TERM_AND_LOG;
  240.         str_room(1);
  241.         format_ident = make_string();
  242.         pack_job_name(str_fmt);
  243.         while ((fmt_file = w_open_out()) == NULL)
  244.                 prompt_file_name("format file name", str_fmt);
  245.         print_nl("Beginning to dump on file ");
  246.         print_str(w_make_name_string(fmt_file));
  247.         flush_string();
  248.         print_nl("");
  249.         print_str(format_ident);
  250.  
  251.         dump_int(MEM_BOT);
  252.         dump_int(MEM_TOP);
  253.         dump_int(TOK_BOT);
  254.         dump_int(TOK_TOP);
  255.         dump_int(EQTB_SIZE);
  256.         dump_int(HASH_PRIME);
  257.         dump_int(HYPH_SIZE);
  258.  
  259.         dump_int(pool_ptr);
  260.         dump_int(str_ptr);
  261.         dump_int(null_str);
  262.         for (k = 0; k <= str_ptr; incr(k))
  263.                 dump_int(str_start[k]);
  264.         for (k = 0; k < pool_ptr; incr(k))
  265.                 dump_ascii();
  266.         print_ln();
  267.         print_int(str_ptr);
  268.         print(" strings of total length ");
  269.         print_int(pool_ptr);
  270.  
  271.         sort_avail();
  272.         var_used = 0;
  273.         dump_int(lo_mem_max);
  274.         dump_int(rover);
  275.         p = MEM_BOT;
  276.         q = rover;
  277.         x = 0;
  278.         do {
  279.                 for (k = p; k <= q + 1; incr(k))
  280.                         dump_wd(mem[k]);
  281.                 x = x + q + 2 - p;
  282.                 var_used = var_used + q - p;
  283.                 p = q + node_size(q);
  284.                 q = rlink(q);
  285.         } while (q != rover);
  286.         var_used = var_used + lo_mem_max - p;
  287.         dyn_used = mem_end + 1 - hi_mem_min;
  288.         for (k = p; k <= lo_mem_max; incr(k))
  289.                 dump_wd(mem[k]);
  290.         x = x + lo_mem_max + 1 - p;
  291.         dump_int(hi_mem_min);
  292.         dump_int(avail);
  293.         for (k = hi_mem_min; k <= mem_end; incr(k))
  294.                 dump_wd(mem[k]);
  295.         x = x + mem_end + 1 - hi_mem_min;
  296.         for (p = avail; p != NULL; p = link(p))
  297.                 decr(dyn_used);
  298.         tok_used = tok_end + 1 - tok_low;
  299.         dump_int(tok_low);
  300.         dump_int(tok_head);
  301.         for (k = tok_low; k <= tok_end; incr(k)) {
  302.                 dump_wd(tok_link[k]);
  303.                 dump_wd(tok[k]);
  304.         }
  305.         for (p = tok_head; p != NULL; p = token_link(p))
  306.                 decr(tok_used);
  307.         x = x + tok_end + 1 - tok_low;
  308.         dump_int(var_used);
  309.         dump_int(dyn_used);
  310.         dump_int(tok_used);
  311.         print_ln();
  312.         print_int(x);
  313.         print(" memory locations dumped; current usage is ");
  314.         print_int(var_used);
  315.         print_char(',');
  316.         print_int(dyn_used);
  317.         print_char(',');
  318.         print_int(tok_used);
  319.  
  320.         k = ACTIVE_BASE;
  321.         do {
  322.                 for (j = k; j < INT_BASE - 1; incr(j))
  323.                         if (equiv(j) == equiv(j + 1) &&
  324.                                 eq_type(j) == eq_type(j + 1) &&
  325.                                 eq_level(j) == eq_level(j + 1))
  326.                                 goto found1;
  327.                 l = INT_BASE;
  328.                 goto done1;
  329.         
  330.         found1:
  331.                 incr(j);
  332.                 l = j;
  333.                 for (; j < INT_BASE - 1; incr(j))
  334.                         if (equiv(j) != equiv(j + 1) ||
  335.                                 eq_type(j) != eq_type(j + 1) ||
  336.                                 eq_level(j) != eq_level(j + 1))
  337.                                 goto done1;
  338.         
  339.         done1:
  340.                 dump_int(l - k);
  341.                 for (; k < l; incr(k))
  342.                         dump_wd(eqtb[k]);
  343.                 k = j + 1;
  344.                 dump_int(k - l);
  345.         } while (k != INT_BASE);
  346.  
  347.         do {
  348.                 for (j = k; j < EQTB_SIZE; incr(j))
  349.                         if (eqtb[j].i == eqtb[j + 1].i)
  350.                                 goto found2;
  351.                 l = EQTB_SIZE + 1;
  352.                 goto done2;
  353.  
  354.         found2:
  355.                 incr(j);
  356.                 l = j;
  357.                 for (; j < EQTB_SIZE; incr(j))
  358.                         if (eqtb[j].i != eqtb[j + 1].i)
  359.                                 goto done2;
  360.         
  361.         done2:
  362.                 dump_int(l - k);
  363.                 for (; k < l; incr(k))
  364.                         dump_wd(eqtb[k]);
  365.                 k = j + 1;
  366.                 dump_int(k - l);
  367.         } while (k <= EQTB_SIZE);
  368.         dump_int(par_loc);
  369.         dump_int(write_loc);
  370.         
  371.         dump_int(hash_used);
  372.         cs_count = FROZEN_CONTROL_SEQUENCE - 1 - hash_used;
  373.         for (p = HASH_BASE; p <= hash_used; incr(p))
  374.                 if (text(p) != 0) {
  375.                         dump_int(p);
  376.                         dump_hh(hash[p]);
  377.                         incr(cs_count);
  378.                 }
  379.         for (p = hash_used + 1; p < UNDEFINED_CONTROL_SEQUENCE; incr(p))
  380.                 dump_hh(hash[p]);
  381.         dump_int(cs_count);
  382.         print_ln();
  383.         print_int(cs_count);
  384.         print(" multiletter control sequences");
  385.  
  386.         dump_int(fmem_ptr);
  387.         for (k = 0; k < fmem_ptr; incr(k))
  388.                 dump_wd(font_info[k]);
  389.         dump_int(font_ptr);
  390.         for (k = NULL_FONT; k <= font_ptr; incr(k)) {
  391.                 dump_qqqq(font_check[k]);
  392.                 dump_wd(font_size[k]);
  393.                 dump_wd(font_dsize[k]);
  394.                 dump_int(font_params[k]);
  395.                 dump_int(hyphen_char[k]);
  396.                 dump_int(skew_char[k]);
  397.                 dump_int(font_name[k]);
  398.                 dump_int(font_area[k]);
  399.                 dump_int(font_bc[k]);
  400.                 dump_int(font_ec[k]);
  401.                 dump_int(char_base[k]);
  402.                 dump_int(width_base[k]);
  403.                 dump_int(height_base[k]);
  404.                 dump_int(depth_base[k]);
  405.                 dump_int(italic_base[k]);
  406.                 dump_int(lig_kern_base[k]);
  407.                 dump_int(kern_base[k]);
  408.                 dump_int(exten_base[k]);
  409.                 dump_int(param_base[k]);
  410.                 dump_int(font_glue[k]);
  411.                 print_nl("\\font");
  412.                 print_esc("");
  413.                 print_str(font_id_text(k));
  414.                 print_char('=');
  415.                 print_file_name(font_name[k], font_area[k], null_str);
  416.                 if (font_size[k] != font_dsize[k]) {
  417.                         print(" at ");
  418.                         print_scaled(font_size[k]);
  419.                         print("pt");
  420.                 }
  421.         }
  422.         print_ln();
  423.         print_int(fmem_ptr - 7);
  424.         print(" words of font info for ");
  425.         print_int(font_ptr - FONT_BASE);
  426.         print(" preloaded font");
  427.         if (font_ptr != FONT_BASE + 1)
  428.                 print_char('s');
  429.         dump_int(hyph_count);
  430.         for (k = 0; k <= HYPH_SIZE; incr(k))
  431.                 if (hyph_word[k] != 0) {
  432.                         dump_int(k);
  433.                         dump_int(hyph_word[k]);
  434.                         dump_int(hyph_list[k]);
  435.                 }
  436.         dump_int(trie_max);
  437.         for (k = 0; k <= trie_max; incr(k))
  438.                 dump_hh(trie[k]);
  439.         dump_int(trie_op_ptr);
  440.         for (k = MIN_QUARTERWORD + 1; k <= trie_op_ptr; incr(k)) {
  441.                 dump_int(hyf_distance[k]);
  442.                 dump_int(hyf_num[k]);
  443.                 dump_int(hyf_next[k]);
  444.         }
  445.         print_ln();
  446.         print_int(hyph_count);
  447.         print(" hyphenation exception");
  448.         if (hyph_count != 1)
  449.                 print_char('s');
  450.         print_nl("Hyphenation trie of length ");
  451.         print_int(trie_max);
  452.         print(" has ");
  453.         print_int(qo(trie_op_ptr));
  454.         print(" op");
  455.         if (trie_op_ptr != MIN_QUARTERWORD + 1)
  456.                 print_char('s');
  457.  
  458.         dump_int(interaction);
  459.         dump_int(format_ident);
  460.         dump_int(6969);
  461.         w_close(fmt_file);
  462.         tracing_stats = 0;
  463. }
  464. #endif
  465.